﻿using Dora.Core.System;
using Dora.Orm.SqlSugarCore.UnitOfWork;
using Dora.Services.AdminUser.Entities;
using Dora.Services.AdminUser.Repository.Repositorys;
using Dora.Tools.Enums;
using Dora.Tools.Enums.AdminUser;
using Dora.Tools.Events.LogsEvents;
using Dora.Tools.Utility;
using Dora.Tools.Utility.Const;
using Newtonsoft.Json;
using Surging.Core.ProxyGenerator;
using System;
using System.Threading.Tasks;

namespace Dora.Services.AdminUser.Application.Base
{
    public class DoraServiceBase : DoraProxyServiceBase
    {
        private readonly ISugarUnitOfWork _unitOfWork;

        public DoraServiceBase()
        {
        }

        public DoraServiceBase(ISugarUnitOfWork unitOfWork)
        {
            _unitOfWork = unitOfWork;
        }

        /// <summary>
        /// 写登录日志
        /// </summary>
        /// <param name="EmployeeId"></param>
        /// <param name="UserName"></param>
        /// <param name="RealName"></param>
        /// <param name="Ip"></param>
        /// <param name="Remarks"></param>
        /// <param name="Type"></param>
        public Task WriteLoginLog(string systemKey,int orgId, Guid UserId, string UserName, string RealName, string Ip,  UserLoginType loginType)
        {
            return Task.Run(() =>
            {
                var msgEvent = new SysLogEvent()
                {
                    LogType = Tools.Enums.SysLog.LogType.Login,
                    Body = JsonConvert.SerializeObject(new LoginLogBody()
                    {
                        SystemKey = systemKey,
                        OrgId= orgId,
                        LoginType =(short)loginType.GetHashCode(),
                        UserId = UserId,
                        UserName = UserName,
                        RealName = RealName,
                        IP=Ip                        
                    })
                };
                Publish(msgEvent);
            });
        }

        #region 日志
        /// <summary>
        /// 写操作日志
        /// </summary>
        public Task WriteOperationLog(OperationType Type, object ParamValue, Guid OpId, string OpName, string Remarks = "")
        {
            return Task.Run(() => {
                var paramJson = JsonConvert.SerializeObject(ParamValue);

                var msgEvent = new SysLogEvent()
                {
                    LogType = Tools.Enums.SysLog.LogType.Operation,
                    Body = JsonConvert.SerializeObject(new OperationLogBody()
                    {
                        SystemKey = DoraModuleName.AdminUser,
                        OpType = (short)Type,
                        ParamValue = paramJson,
                        OpTime = DateTime.Now,
                        OpUserId = OpId,
                        OpUserName = OpName,
                        Remark = Remarks,
                    })
                };
                Publish(msgEvent);
            });
        }

        #endregion
    }
}
